<?php
namespace App\Models\Scripts;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class v30db597822c6e06c6737052260426f45 extends Sysupdate
{
    protected $version_id ='initialize';

    private $table_name = 'sys_version';
    private $returnData = array('code'=>0,'message'=>'执行成功');

    public function upgrade()
    {
        $r_data = $this->_step_1();
        if($r_data['code']>0){
            return $r_data;
        }
        $r_data = $this->_step_2();
        if($r_data['code']>0){
            return $r_data;
        }
        $r_data = $this->_step_3();
        if($r_data['code']>0){
            return $r_data;
        }
        return $r_data;
    }

    private function _step_1()
    {
        try {
            //检查是否完成
            $res = DB::table($this->table_name)->get();
            if($res){
                $this->returnData['code'] = 101;
                $this->returnData['message'] = 'table '.$this->table_name.' exist';
                return $this->returnData;
            }
        } catch(\Illuminate\Database\QueryException $e) {
            $errorInfo = json_decode(json_encode($e->errorInfo),true);
            if($errorInfo[0]!='42S02'){
                Log::info('table '.$this->table_name.' error:',array('errorinfo'=>json_encode($e)));
                $this->returnData['code'] = 102;
                $this->returnData['message'] = json_encode($e);
                return $this->returnData;
            }
            $sql = "CREATE TABLE `sys_version` (".
                "  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',".
                "  `version` varchar(100) DEFAULT NULL COMMENT 'crm版本',".
                "  `status` tinyint(1) DEFAULT NULL COMMENT '代码更新状态:0 未更新/1 未执行/2 已更新',".
                "  `tips` varchar(200) DEFAULT NULL COMMENT '注意提示',".
                "  `introduce` longtext COMMENT '版本介绍(html code)',".
                "  `create_time` int(11) DEFAULT NULL COMMENT '创建时间戳',".
                "  PRIMARY KEY (`id`)".
                ") ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;";
            DB::statement($sql);
            return $this->returnData;
        }
        return $this->returnData;
    }

    private function _step_2()
    {
        try {
            //检查有数据
            $res = DB::table($this->table_name)->where('version',$this->version_id)->first();
            if($res){
                $this->returnData['code'] = 103;
                $this->returnData['message'] = $this->version_id.' exist';
                return $this->returnData;
            }
        } catch(\Illuminate\Database\QueryException $e) {
            $errorInfo = json_decode(json_encode($e->errorInfo),true);
            if($errorInfo[0]=='42S02'){
                $this->returnData['code'] = 104;
                $this->returnData['message'] = 'step 1 create table '.$this->table_name.' fail.';
                return $this->returnData;
            }
        }
        /* update code start*/
        $data = array(
            'version' => $this->version_id,
            'status' => 2,
            'tips' => $this->_buildTips(),
            'introduce' => $this->_buildIntroduce(),
            'create_time' => $this->timestamp
        );
        $res = DB::table($this->table_name)->insert($data);
        if(!$res){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = $this->version_id.'更新失败';
            return $this->returnData;
        }
        return $this->returnData;
    }

    private function _step_3()
    {
        //admin.sysupdate.index
        $res = DB::table('admin_permissions')->where('name','LIKE','%admin.configs%')->where('cid',0)->first();
        $res = json_decode(json_encode($res),true);
        $data = array(
            'name' => 'admin.sysupdate.index',
            'label' => 'CRM版本',
            'cid' => $res['id'],
            'display' => 0,
            'sort' => 0,
            'created_at' => $this->time,
            'updated_at' => $this->time
        );
        $flag = DB::table('admin_permissions')->insert($data);
        if(!$flag){
            $this->returnData['code'] = 99;
            $this->returnData['message'] = '添加admin.sysupdate.index失败';
            return $this->returnData;
        }
        $res = $this->clearCache();
        $this->returnData['data'] = $res;
        return $this->returnData;
    }

    private function _buildTips()
    {
        $content = "初始化Crm迭代更新模块";
        return $content;
    }

    private function _buildIntroduce()
    {
        $html_str = '<ul>';
        $html_str .= '    <li>';
        $html_str .= '        <h3>1.crm 更新模块介绍</h3>';
        $html_str .= '        <ul>';
        $html_str .= '            <li>1.1 利用laravel自带php artisan command生成命令 生成Runscripts</li>';
        $html_str .= '            <pre class="layui-code">';
        $html_str .= '/* command 生成Runscripts */';
        $html_str .= '<br>php artisan make:command RunScripts --command=cmd:runscripts';
        $html_str .= '</pre>';
        $html_str .= '            <li>1.2 将RunScripts 添加进Kerner.php内</li>';
        $html_str .= '            <pre class="layui-code">';
        $html_str .= '/* Kerner.php内修改 */';
        $html_str .= '<br>use App\Console\Commands\RunScripts;';
        $html_str .= '<br>...';
        $html_str .= '<br>protected $commands = [';
        $html_str .= 'RunScripts::class';
        $html_str .= '];';
        $html_str .= '</pre>';
        $html_str .= '            <li>1.3 上传更新的model文件 更新的model放进\app\model\scripts内 以v开头拼接md5后的version md5(version_id)</li>';
        $html_str .= '            <li>1.4 执行命令</li>';
        $html_str .= '<pre class="layui-code">';
        $html_str .= '/* 执行更新 initialize为对应的版本号 */';
        $html_str .= '<br>php artisan cmd:runscripts --v=initialize';
        $html_str .= '</pre>';
        $html_str .= '        </ul>';
        $html_str .= '    </li>';
        $html_str .= '    <li>';
        $html_str .= '        <h3>2.更新内容</h3>';
        $html_str .= '        <ul>';
        $html_str .= '            <li>2.1 step1 建立数据库sys_version</li>';
        $html_str .= '            <li>2.2 step2 执行脚本(插入initialize的更新内容入数据库)</li>';
        $html_str .= '        </ul>';
        $html_str .= '    </li>';
        $html_str .= '    <li>';
        $html_str .= '        <h3>3.后续待完善功能</h3>';
        $html_str .= '        <ul>';
        $html_str .= '            <li>3.1富文本编辑器 用于编辑introduce</li>';
        $html_str .= '            <li>3.2介绍文档编辑功能 Crm version <a href="javascript:void(0)" target="_blank">#readme.md</a></li>';
        $html_str .= '        </ul>';
        $html_str .= '    </li>';
        $html_str .= '</ul>';
        return $html_str;
    }
}
